/*
第七页 262666212@qq.com
7
*/
/*
"i am is {0} age is {1}".format("diqye",22);
 out: i am is diqye age is 22
*/
//string扩展 个人使用习惯
String.prototype.format=function(p1){
	var args=arguments;
	if(args.length==1&& typeof p1 == "object") args=p1;
	return this.replace(/\{([0-9]+)\}/g,function(all,a){
		return args[a];
	});
};
/*
    new Date(2012,1,1,10,9).format('yyyyMMddhhmmss)
    out: 201201011009
*/
// yyyy MM dd hh mm ss
Date.prototype.format=function(yyyyMMdd){
	var month=this.getMonth()+1
	,date=this.getDate()
	,hours=this.getHours()
	,min=this.getMinutes()
	,sec=this.getSeconds();
	return yyyyMMdd.replace(/yyyy/g,this.getFullYear())
	.replace(/yy/g,String(this.getFullYear()).substr(2,2))
	.replace(/MM/g,month>=10?month:"0"+month)
	.replace(/M\*/g,month)
	.replace(/dd/g,date>=10?date:"0"+date)
	.replace(/d\*/g,date)
	.replace(/hh/g,hours>=10?hours:"0"+hours)
	.replace(/h\*/g,hours)
	.replace(/m\*/g,min)
	.replace(/mm/g,min>=10?min:"0"+min)
	.replace(/ss/g,sec>=10?sec:"0"+sec)
	.replace(/s\*/g,sec)
};
/*
  var delay1000=delay(1000);
  delay1000(function(){...});

*/
function delay(long){
    long=long||1000;
    var timeid=null;
    return function(fn){
        clearTimeout(timeid);
        timeid=setTimeout(fn,long);
    }
}
/*
   预先设置函数的参数
   function fn(a){console.log(a)};
   var fn1=util.curry(fn,'a');
   fn1();
   out:a
*/
function curry(fn){//函数柯里化
	var args = [].slice.call(arguments, 1);
	return function(){
		var newArgs = args.concat([].slice.call(arguments));
		return fn.apply(this, newArgs);
	}
}
/*
   绑定函数的上下文
   function fn(){console.log(this)};
   util.bind(fn,"aa")();
   out:aa
*/
function bind(fn,scope){//绑定函数
	return function(){
		var args = [].slice.call(arguments, 0);
		return fn.apply(scope, args);
	}
}
function each(arr,fn){
    if(!arr||!arr.length)return;
	for(var i=0,l=arr.length,t;i<l;i++){
		t=arr[i];
		if(fn(t,i)===false)break;
	}
}
/*
   var pubsub=util.pubsub();
   绑定事件event1 fn为回调函数
   pubsub.on('event1',fn);
   与前者不同的是 one只执行一次
   pubsub.one('event1',fn);
   触发事件 并传参数
   pubsub.emit('event1',arg1，arg2);
*/
function pubsub(){
	var topic={};
	// key1:key2:key3
	function on(key,fn){
		var keys=key.split(":");
		if(keys.length!=1){
			each(keys,function(t){
				on(t,fn);
			});
			return;
		}
		(topic[key]=topic[key]||[]).push(fn);
	}
	function once(key,fn){
		fn._one_=true;
		on(key,fn);
	}
	//key1:key2:key3
	function off(key,fn){
		var keys=key.split(":");
		if(keys.length!=1){
			each(keys,function(t){
				off(t,fn);
			});
			return;
		}
		if(fn==null)topic[key]=null;
		else for(var arr=topic[key],l=arr.length-1,t;l>=0;l--){
			t=arr[l];
			if(fn==t)arr.splice(l,1);
		}
	}
	function emit(key){
		var keys=key.split(":");
		if(keys.length!=1){
			each(keys,function(t){
				emit(t);
			});
			return;
		}
		var args=[].slice.call(arguments, 0);
		if(topic[key]==null)return;
		for(var arr=topic[key],i=0,l=arr.length,t;i<l;i++){
			t=arr[i];
			t.apply(null,args.slice(1));
			if(t._one_){
				arr.splice(i,1);
				i--;
				l--;
			}
		}
	}
  
    function init(obj){
      obj.on=on;
      obj.once=once;
      obj.off=off;
      obj.emit=emit;
    }

	return {
		once:once,
		on:on,
        init:init,
		off:off,
		emit:emit
	}
}
//数据类型判断
var type=(function(){
	var r={},types=['Arguments','Function','String','Number','Date','RegExp','Error','Null','Array'];
	for(var i=0,t;t=types[i++];){
		!function(t){
			r['is'+t]=function(obj){
				return Object.prototype.toString.call(obj) === '[object '+t+']';
			}
		}(t)
	}
	return r;
})();